using System;
using System.Collections.Generic;
using System.Text;

using phorm.database.core;

namespace phorm.model.fields
{
    /// <summary>
    /// A UNIX timestamp which is the number of seconds since 1/1/1970 12:00 AM
    /// </summary>
    public class UnixTimestamp : Integer
    {
        /// <summary>
        /// Constant default length of this column
        /// </summary>
        const int DEFAULT_LENGTH = 11;

        /// <summary>
        /// Constructor relies on base for object construction.
        /// </summary>
        /// <param name="obj">The model object to which this column belongs</param>
        public UnixTimestamp(DatabaseRecord obj)
            : base(obj, UnixTimestamp.DEFAULT_LENGTH)
        {
        }

        /// <summary>
        /// Method that calculates the current timestamp.
        /// </summary>
        /// <returns>Current UNIX timestamp</returns>
        public int now()
        {
            TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
            return (int)ts.TotalSeconds;
        }
    }
}
